package ch06;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

public class PersonService {
    public void before(JoinPoint jp) {
        System.out.println("==========前置通知开始============");
        System.out.println("调用 " + jp.getTarget() + " 的 " + jp.getSignature().getName()
                + " 方法。方法入参：" + Arrays.toString(jp.getArgs()));
        System.out.println("==========打开数据库连接=============");
    }

    public void a(JoinPoint jp, Object result) {
        System.out.println("==============后置通知开始===============");
        System.out.println(jp.getTarget() + "---" + jp.getSignature().getName() + "--" + jp.getArgs() + "--" + result);
        System.out.println("提交事务");
        System.out.println("关闭数据库连接");
    }

    public void b(JoinPoint jp, RuntimeException e) {
        System.out.println(jp.getSignature().getName() + " 方法发生异常：" + e);
        System.out.println("捕捉到异常 " + e.getMessage());
    }

    public void c (JoinPoint jp) {
        System.out.println(jp.getSignature().getName() + " 方法结束执行。");
        System.out.println("没有返回值的增强");
    }

    public Object d (ProceedingJoinPoint jp) throws Throwable {
        try {
            System.out.println("打开数据库连接");
            Object result = jp.proceed();
            System.out.println("提交事务与关闭数据库连接");
            return result;
        } catch (Throwable e) {
            System.out.println(jp.getSignature().getName() + " 方法发生异常：" + e);
            throw e;
        } finally {
            System.out.println(jp.getSignature().getName() + " 方法结束执行。");
        }

    }

}
